package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfiguration {
    @Bean//其实我们有启动依赖这个resdiTemp，但我们想要设置连接工厂、设置key序列化器，所以我们需要自己重新创建redisTemplate
    @SuppressWarnings("all")
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory/*连接工厂*/){
        log.info("开始设置redis模板对象");
        RedisTemplate redisTemplate=new RedisTemplate();
        //设置连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置key的序列化器-不设置的话在redis服务中key会乱码，一样的,value不设置的话也一样会乱码。但他们查询上来的没问题的
        redisTemplate.setKeySerializer(new StringRedisSerializer()/*字符串类型的redis序列化器*/);
//        redisTemplate.setKeySerializer(new JdkSerializationRedisSerializer());
        //设置value的序列化器
//        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
